// pages/first/first.js
let app = getApp()
let connector = require('../../utils/connector.js')
let setTime;
//goTo:1:首页；2：商城；3商品详情；
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gif:"true",//加载动画
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    
    const scene = decodeURIComponent(options.scene)
    console.log(scene)
    setTime = setInterval(function(){
      // console.log(app.globalData.identity)
      if(app.globalData.isYes){
        let pid;
        clearInterval(setTime)
        if(options.scene){
          let indexOfInd = scene.indexOf("pid_")
          // console.log(indexOfInd)
          // console.log(scene.slice(indexOfInd+4))
          userFather(that, {pid:scene.slice(indexOfInd+4)})
          //扫码
          console.log("扫码")
          wx.switchTab({
            url: '/pages/shopping/shopping',
          })
        }else{
          if(options.pid){
            console.log("分享")
            userFather(that, {pid:options.pid})
            //首页
            if(options.goTo < 3){
              let goTo="/pages/shopping/shopping"
              if(options.goTo == 1){
                goTo="/pages/shopping/shopping"
              }
              //商城
              if(options.goTo == 2){
                goTo="/pages/index/index"
              }
              wx.switchTab({
                url: goTo,
              })
            }else{
              let goTo2="/pages/shopping/shopping"
              //商品
              if(options.goTo == 3){
                app.globalData.shoppingId = options.shop
                goTo2="/pages/index/particulars/particulars"
              }
              wx.redirectTo({
                url: goTo2,
              })
            }
            
    
          }else{
            console.log("搜索")
            wx.switchTab({
              url: '/pages/shopping/shopping',
            })
          }
          console.log("不知道")
        }
      }
    },100)
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("监听页面卸载")
    if(setTime){
      clearInterval(setTime)
    }
  },
})
//绑定父级 
function userFather(that, obj) {
  console.log(obj)
  connector.getFunc(connector.connector().userFather, obj, "post", app.tokens, function (res) {
    console.log(res)
    // if (res.data.result) {
    //   // console.log("11111111111")
    //   // that.setData({
    //   //   collected:true
    //   // })
    // }
  }, function (res) {
    wx.showToast({
      title: "内部错误",
      icon: 'none',
      duration: 1000
    })
  })
}